package com.hs.controller;


import com.github.pagehelper.PageInfo;
import com.hs.entity.RestBean;
import com.hs.entity.dto.Notice;
import com.hs.service.NoticeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @GetMapping("/getNoticeList/{currentNum}")
    public RestBean<PageInfo<Notice>> getNoticeList(
            @PathVariable("currentNum") Integer currentNum,
            @RequestParam(required = false) String keywords) {
        PageInfo<Notice> noticeList = noticeService.selectNoticeList(currentNum,keywords);
        return RestBean.success(noticeList);
    }

    @GetMapping("/getNoticeById/{id}")
    public RestBean<Notice> getNoticeById( @PathVariable Integer id) {
        System.out.println(id);

        Notice notice = noticeService.getById(id);
        return RestBean.success(notice);
    }

    @PostMapping("/updateNotice")
    public RestBean<Notice> updateNotice( @RequestBody Notice notice) {
        System.out.println(notice);
        noticeService.updateById(notice);
        return RestBean.success(null);
    }



    @PostMapping("/addNotice")
    public RestBean<Notice> addNotice( @RequestBody Notice notice) {
        notice.setCreate_time(LocalDateTime.now());
        noticeService.save(notice);
        return RestBean.success(null);
    }

    @PostMapping("/deleteNotice/{id}")
    public RestBean<Notice> deleteNotice(@PathVariable  Integer id) {
        noticeService.removeById(id);
        return RestBean.success(null);
    }



}
